<%--
  Created by IntelliJ IDEA.
  User: user
  Date: 11.05.12
  Time: 18:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page import="db.DBConnection" %>
<%@ page import="db.User" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="db.Account" %>
<%@ page import="java.awt.image.AreaAveragingScaleFilter" %>
<%@ page import="db.Message" %>
<%
    int user_id = -1;
    String secret1 = "";
    Cookie[] cookies = request.getCookies();

    for (Cookie cookie : cookies) {
        if (cookie.getName().equals("user_id")) {
            user_id = Integer.parseInt(cookie.getValue());
        } else if (cookie.getName().equals("secret")) {
            secret1 = cookie.getValue();
        }
    }
    DBConnection dbConnection = new DBConnection();
    if (!dbConnection.isCookieOk(user_id, secret1)) {
        String redirectURL = "../index.jsp";
        response.sendRedirect(redirectURL);
    }

    String markRead = request.getParameter("markRead");
    String markUnread = request.getParameter("markUnread");

    if (markRead != null) {
        int id = Integer.parseInt(markRead);
        dbConnection.doMarkMessageRead(user_id, id);
    }
    if (markUnread != null) {
        int id = Integer.parseInt(markUnread);
        dbConnection.doMarkMessageUnread(user_id, id);
    }
    String id = request.getParameter("id");
    int message_id = 0;
    if (id != null) {
        message_id = Integer.parseInt(id);
    }
    
    int num = dbConnection.doGetNumberOfUnreadMessages(user_id);
    
    Message message = dbConnection.getMessageByID(user_id, message_id);
%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Messages</title>
    <link rel="stylesheet" type="text/css" href="../style"/>
</head>

<body>
<div class="logout" style="text-align:right;"><a href="../logout.jsp">  Logout</a></div>
<div class="header"><img src="../logo.jpg"></div>
<div class="layout">
    <div class="sidebar">
        <ul class="cssmenu">
            <li><a href="personal_info.jsp">Personal info</a></li>
            <li><a href="operations.jsp">Operations</a></li>
            <li><b><a href="messages.jsp">Messages<% if (num != 0) {%> <b> (<%=num%>)</b> <%}%></a></b></li>
        </ul>
    </div>
    <table width="900" border="1" align="center" style="word-wrap: break-word">
        <tr>
            <td width="50">Message_id</td>
            <td width="200">Date</td>
            <td width="100">Sender</td>
            <td width="100">Receiver</td>
            <td width="400">Message</td>
            <td width="50">Answer</td>
            <td width="100">Mark</td>
        </tr>
        <tr>
            <td><%=message.message_id%></td>
            <td><%=message.date%></td>
            <td><%=dbConnection.getNameByID(user_id, message.sender_id)%></td>
            <td><%=dbConnection.getNameByID(user_id, message.receiver_id)%></td>
            <td><%=message.message%></td>
            <td align="center">
                <% if (message.answer_id == -1 && message.sender_id != user_id){%>
                <button onclick="location.href='reply.jsp?send_to=<%=message.sender_id%>&message_id=<%=message.message_id%>'"> Reply </button>
                <%} else if (message.answer_id != -1) {%>
                <button onclick="location.href='one_message.jsp?id=<%=message.answer_id%>'"> View </button>
                <%} else {%>
                &#8212;
                <%}%>
            </td>
            <td>
                <%if (user_id != message.sender_id) {%>
                    <%if (!message.isRead) {%>
                        <button onclick="location.href='one_message.jsp?id=<%=message_id%>&markRead=<%=message.message_id%>'"> Mark read </button>
                    <%} else {%>
                        <button onclick="location.href='one_message.jsp?id=<%=message_id%>&markUnread=<%=message.message_id%>'"> Mark unread </button>
                    <%}%>
                <%} else {%>
                    Sent message
                <%}%>
            </td>
        </tr>
    </table>
    <div class="content">
    </div>
</div>
</body>
</html>